//
//  ClassificationMusicTableViewController.m
//  K歌之王
//
//  Created by 小晓 on 16/1/3.
//  Copyright © 2016年 xiao. All rights reserved.
//

#import "MusicClassTableViewController.h"
#import "MusicClassTableViewCell.h"
#import "SingerTableViewController.h"

@interface MusicClassTableViewController ()

@property (strong,nonatomic)NSMutableArray * curDataArray;
@property (strong,nonatomic)NSMutableArray * curIdArray;

@end

@implementation MusicClassTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.titleName;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.curDataArray = [[NSMutableArray alloc]init];
    self.curIdArray = [[NSMutableArray alloc]init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if([_titleName isEqualToString:@"歌星点歌"]){
        self.tableView.scrollEnabled = NO;
        [self requestDataWithString:@"/api/singer_category"];
    }else if ([_titleName isEqualToString:@"分类点歌"]){
        self.tableView.scrollEnabled = YES;
        [self requestDataWithString:@"/api/songCategorys"];
    }
}

-(void)viewWillAppear:(BOOL)animated{
     self.tabBarController.tabBar.hidden = YES;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(backBtnAction)];
}

-(void)backBtnAction{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)requestDataWithString:(NSString *)urlStr{
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:urlStr];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSDictionary * bodyDict = [dic objectForKey:@"body"];
        NSArray * contentArray = [bodyDict objectForKey:@"content"];
        for(NSDictionary * contentDict in contentArray){
            NSString * name = [contentDict objectForKey:@"name"];
            NSString * titleID = [contentDict objectForKey:@"id"];
            [_curDataArray addObject:name];
            [_curIdArray addObject:titleID];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.curDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicClassTableViewCell"];
    if ( !cell ) {
        cell = [[MusicClassTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MusicClassTableViewCell"];
    }
    [(MusicClassTableViewCell *)cell updataCellViewWithHeadData:[_curDataArray objectAtIndex:indexPath.row] Style:1];
    return cell;
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SingerTableViewController * singerTabelViewCtr = [[SingerTableViewController alloc]init];
    singerTabelViewCtr.className = self.titleName;
    singerTabelViewCtr.titleID = [self.curIdArray objectAtIndex:indexPath.row];
    singerTabelViewCtr.titleName = [self.curDataArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:singerTabelViewCtr animated:YES];
}

@end
